﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AutoUpdate.Utility.Helper
{
    using Newtonsoft.Json.Linq;
    using System.Text.RegularExpressions;
    using System.Drawing;
    using System.Drawing.Drawing2D;

    public class UIUtils
    {

        /// <summary>
        /// 返回当前毫秒时间错
        /// </summary>
        /// <returns></returns>
        public static long CurrentTimeMillis()
        {
            var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            long times = Convert.ToInt64((ts.TotalSeconds + TimeUtils.SyncTimeDiff()) * 1000);
            return times;
        }

        public static double CurrentTimeDouble()
        {
            var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return ts.TotalSeconds + TimeUtils.SyncTimeDiff();
        }
        public static long CurrentTimeLong()
        {
            var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds + TimeUtils.SyncTimeDiff());

        }
        public static bool IsNull(string str)
        {
            if (str == null || str.Length == 0)
            {
                return true;
            }

            return false;

        }

        public static bool IsNull<T>(List<T> str)
        {

            if (str == null || str.Count == 0)
            {
                return true;
            }

            return false;

        }


        public static bool IsNull(Dictionary<string, string> data)
        {

            if (data == null || data.Count == 0)
            {
                return true;
            }
            return false;
        }

        internal static string LimitTextLength(string value, int max, bool Ellipsis)
        {
            if (string.IsNullOrEmpty(value))
            {
                return value;
            }
            if (Ellipsis)
            {
                return value.Length > max ? value.Substring(0, max) + "..." : value;
            }
            return value.Length > max ? value.Substring(0, max) : value;
        }


        internal static string MaxTextLine(string value, int max)
        {
            string[] sArray = Regex.Split(value, "\n", RegexOptions.IgnoreCase);
            if (sArray != null && sArray.Length > max)
            {
                string res = "";
                for (int i = 0; i < max; i++)
                {
                    res = res + sArray[i];
                }

                return res;
            }

            return value;
        }


        internal static string DecodeString(JToken pairs, string key)
        {
            try
            {
                return pairs[key].ToString();
            }
            catch (Exception)
            {
                Console.WriteLine("DecodeString 解析失败" + key);
                return "";
            }
        }

        internal static long DecodeLong(JToken pairs, string key)
        {
            try
            {
                long val = pairs.Value<long>(key);
                return val;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        internal static int DecodeInt(JToken pairs, string key)
        {

            try
            {
                int val = pairs.Value<int>(key);
                return val;
            }
            catch (Exception)
            {
                return 0;
            }
        }


        //internal static double DecodeDouble(JToken pairs, string key)
        //{
        //    if (pairs.Contains(key))
        //    {
        //        return Convert.ToDouble(pairs[key]);
        //    }

        //    return 0;
        //}

        internal static string DecodeString(Dictionary<string, object> pairs, string key)
        {
            if (pairs.ContainsKey(key))
            {
                return pairs[key].ToString();
            }

            return "";
        }

        internal static int DecodeInt(Dictionary<string, object> pairs, string key)
        {
            if (pairs.ContainsKey(key))
            {
                return Convert.ToInt32(pairs[key].ToString());
            }

            return 0;
        }
        internal static double DecodeDouble(Dictionary<string, object> pairs, string key)
        {
            if (pairs.ContainsKey(key))
            {
                return Convert.ToDouble(pairs[key].ToString());
            }

            return 0;
        }
        internal static long DecodeLong(Dictionary<string, object> pairs, string key)
        {
            if (pairs.ContainsKey(key))
            {
                return Convert.ToInt64(pairs[key].ToString());
            }

            return 0;
        }

        internal static string QuotationName(string name)
        {
            return "“" + name + "”";
        }

        internal static double DoubleParse(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                return 0;
            }

            return Convert.ToDouble(value);
        }
      

        /// <summary>
        /// 文件大小格式化，
        /// </summary>
        /// <param name="size"> b单位的文件大小 </param>
        /// <returns></returns>

        public static string FromatFileSize(long size)
        {
            if (size < 1024)
            {
                return size + "B";
            }
            else if (size < 1024 * 1024)
            {
                float kbsize = size / 1024f;
                return kbsize.ToString("f2") + "KB";
            }
            else if (size < 1024L * 1024 * 1024)
            {
                float mbsize = size / 1024f / 1024f;
                return mbsize.ToString("f2") + "MB";
            }
            else if (size < 1024L * 1024 * 1024 * 1024)
            {
                float gbsize = size / 1024f / 1024f / 1024f;
                return gbsize.ToString("f2") + "GB";
            }
            else
            {
                return "size: error";
            }

        }

        static string save_url = "";
     
        internal static bool Contains(string text, string str)
        {
            if (!IsNull(text))
            {
                return text.Contains(str);
            }

            return false;
        }

        internal static int HashCode(string oriVideoPath)
        {
            int hash = 0;
            int h = hash;
            int len = oriVideoPath.Length;
            char[] charat = oriVideoPath.ToCharArray();
            if (h == 0 && len > 0)
            {
                for (int i = 0; i < len; i++)
                {
                    h = 31 * h + charat[i];
                }
                hash = h;
            }
            return h;
        }
    }
}
